package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.ApplicationListDTO;
import com.xbongbong.paas.pojo.vo.ApplicationListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formexplain.pojo.vo.ExplainFilterConditionVO;
import com.xbongbong.pro.sharepermission.pojo.dto.BaseSharePermissionDTO;
import com.xbongbong.pro.sharepermission.pojo.dto.ShareFilterConditionDTO;
import com.xbongbong.pro.sharepermission.pojo.dto.SharePermissionListDTO;
import com.xbongbong.pro.sharepermission.pojo.dto.SharePermissionSaveDTO;
import com.xbongbong.pro.sharepermission.pojo.dto.SharePermissionSupportDTO;
import com.xbongbong.pro.sharepermission.pojo.vo.SharePermissionListVO;
import com.xbongbong.pro.sharepermission.pojo.vo.SharePermissionSupportVO;
import com.xbongbong.pro.sharepermission.pojo.vo.SharePermissionVO;
import com.xbongbong.saas.service.ShareRuleService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 共享权限 controller
 *
 * @author leex
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/share")
public class SharePermissionController {
    @Resource
    private ShareRuleService shareRuleService;

    /**
     * 共享规则列表接口
     *
     * @param sharePermissionListDTO 入参
     * @return 接口返回
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid SharePermissionListDTO sharePermissionListDTO, BindingResult br) throws Exception {
        XbbResponse<SharePermissionListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SharePermissionListVO listVO = shareRuleService.list(sharePermissionListDTO);
                response = new XbbResponse<>(listVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 共享规则- get接口
     *
     * @param baseSharePermissionDTO 入参
     * @return 接口返回
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid BaseSharePermissionDTO baseSharePermissionDTO, BindingResult br) throws Exception {
        XbbResponse<SharePermissionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SharePermissionVO getVO = shareRuleService.get(baseSharePermissionDTO);
                response = new XbbResponse<>(getVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 共享规则- add接口
     *
     * @param sharePermissionSaveDTO 入参
     * @return 接口返回
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid SharePermissionSaveDTO sharePermissionSaveDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = shareRuleService.add(sharePermissionSaveDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 共享规则- save接口
     *
     * @param sharePermissionSaveDTO 入参
     * @return 接口返回
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid SharePermissionSaveDTO sharePermissionSaveDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = shareRuleService.save(sharePermissionSaveDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 共享规则- delete接口
     *
     * @param baseSharePermissionDTO 入参
     * @return 接口返回
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid BaseSharePermissionDTO baseSharePermissionDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = shareRuleService.delete(baseSharePermissionDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 共享规则- recalculate接口
     *
     * @param baseDTO 入参
     * @return 接口返回
     */
    @RequestMapping(value = "/recalculate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String recalculate(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = shareRuleService.recalculate(baseDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 共享权限规则-触发条件的字段
     * @param shareFilterConditionDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/conditionList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String conditionList(@RequestBody @Valid ShareFilterConditionDTO shareFilterConditionDTO, BindingResult br) throws Exception {
        XbbResponse<ExplainFilterConditionVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ExplainFilterConditionVO explainFilterConditionVO = shareRuleService.conditionList(shareFilterConditionDTO);
                response = new XbbResponse<>(explainFilterConditionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 共享规则列表接口
     *
     * @param baseDTO 入参
     * @return 接口返回
     */
    @RequestMapping(value = "/isSupport", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String isSupport(@RequestBody @Valid SharePermissionSupportDTO baseDTO, BindingResult br) {
        XbbResponse<SharePermissionSupportVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            SharePermissionSupportVO supportVO = shareRuleService.isSupport(baseDTO);
            response = new XbbResponse<>(supportVO);
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 共享权限 应用列表-支持的应用
     * @param applicationListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/app/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String appList(@RequestBody @Valid ApplicationListDTO applicationListDTO, BindingResult br) throws Exception {
        XbbResponse<ApplicationListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            ApplicationListVO applicationListVO = shareRuleService.appList(applicationListDTO);
            response = new XbbResponse<>(applicationListVO);
        }
        return JSON.toJSONString(response);
    }
}
